package com.chen10.cattle.controller;

import com.chen10.cattle.common.enums.response.SuccessMsgEnum;
import com.chen10.cattle.common.exception.BusinessException;
import com.chen10.cattle.common.protocal.ResponseResult;
import com.chen10.cattle.common.protocal.ResponseVO;
import com.chen10.cattle.model.dto.LoginDTO;
import com.chen10.cattle.model.dto.RegisterDTO;
import com.chen10.cattle.service.AuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/api/auth")
@Slf4j
public class AuthController {
    @Resource
    private AuthService authService;

    /**
     * 登录
     * @param loginDTO
     * @return
     */
    @PostMapping("/login")
    public ResponseVO<Map<String,Object>> login(@RequestBody LoginDTO loginDTO) {
        log.info("用户登录，loginDTO：{}",loginDTO);
        try {
            return authService.login(loginDTO);
        }catch (BusinessException e){
            return ResponseResult.error(e.getCode(), e.getMessage());
        }
    }

    /**
     * 注册
     * @param registerDTO
     * @return
     */
    @PostMapping("/register")
    public ResponseVO register(@RequestBody RegisterDTO registerDTO) {
        log.info("用户注册，registerDTO：{}",registerDTO);
        try {
            authService.register(registerDTO);
            return ResponseResult.success(SuccessMsgEnum.USER_REGISTER_SUCCESS.getMsg());
        }catch (BusinessException e){
            return ResponseResult.error(e.getCode(), e.getMessage());
        }
    }
}

